package com.weixing.mall.provider.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.mapper.SpecValueMapper;
import com.weixing.mall.provider.model.domain.SpecValue;
import com.weixing.mall.provider.model.dto.SpecValueDto;
import com.weixing.mall.provider.model.query.SpecQuery;
import com.weixing.mall.provider.model.query.SpecValueQuery;
import com.weixing.mall.provider.model.vo.SpecValueVo;
import com.weixing.mall.provider.model.vo.SpecVo;
import com.weixing.mall.provider.service.ISpecService;
import com.weixing.mall.provider.service.ISpecValueService;
import com.weixing.mall.provider.support.excel.ColorAndSizeData;
import com.weixing.mall.provider.support.excel.ColorAndSizeDataListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName SpecValueServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/9
 * @Version 1.0
 **/
/**
 * <p>
 * 商品模块-商品规格值 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-20
 */
@Service
public class SpecValueServiceImpl extends ServiceImpl<SpecValueMapper, SpecValue> implements ISpecValueService {
    @Autowired
    ISpecService specService;
    @Autowired
    ISpecValueService specValueService;
    @Override
    public void save(SpecValueDto specValueDto) {
        SpecValue specValue = new SpecValue();
        BeanUtils.copyProperties(specValueDto, specValue);
        specValue.setIsEnable(specValueDto.getIsEnableEnum().getValue());
        if (null == specValueDto.getId()){
            save(specValue);
        }else{
            updateById(specValue);
        }
    }

    @Override
    public IPage<SpecValueVo> selectList(Page page, SpecValueQuery specValueQuery) {
        return baseMapper.selectList(page,specValueQuery);
    }
    /*
    * TODO，今天心情好烦躁写的最烂的代码后期在优化吧，
    *
    * */
    @Override
    public List<SpecValueVo> listTree() {

        List<SpecValueVo> result=tree(new SpecValueQuery());
        return result;
    }

    @Override
    public List<SpecValueVo> treeAbleList(Long  specId) {
        SpecValueQuery query=new SpecValueQuery();
        query.setIsEnable(WhetherEnum.YES);
        query.setSpecId(specId);//系统只有颜色和尺码，这个值可以固定死，前台没有修改的地方，该值不会变
        query.setSpecLevel(1);
        List<SpecValueVo> onelvelList=baseMapper.selectListByLevel(query);
        for(SpecValueVo vo:onelvelList){
            query.setSpecLevel(2);
            query.setParentId(vo.getId());
            vo.setChildList(baseMapper.selectListByLevel(query));
        }
        return onelvelList;
    }

    @Override
    public IPage<SpecValueVo> selectListByLevel(Page initMpPage, SpecValueQuery specValueQuery) {
        return baseMapper.selectListByLevel(initMpPage,specValueQuery);
    }

    @Override
    public List<SpecValueVo> selectListByLevel(SpecValueQuery specValueQuery) {
        return baseMapper.selectListByLevel(specValueQuery);
    }

    @Override
    public SpecValueVo selectSprcByErpId(long specId, String erpId) {
        return baseMapper.selectSprcByErpId(specId,erpId);
    }

    @Override
    public List<SpecValueVo> selectList(SpecValueQuery specValueQuery) {
        return baseMapper.selectList(specValueQuery);
    }

    @Override
    public void init(MultipartFile file) {

        try{
            EasyExcel.read(file.getInputStream(), ColorAndSizeData.class, new ColorAndSizeDataListener(specValueService)).sheet().doRead();
        }catch (Exception e){

        }


    }

    /*
    *
    *
    * */
    private    List<SpecValueVo> tree(SpecValueQuery specValueQuery ){
        List<SpecVo> list=specService.selectList(new SpecQuery());
        List<SpecValueVo> result=Lists.newArrayList();
        List<SpecValueVo> voList= baseMapper.selectList(specValueQuery);
        List<SpecValueVo> voList1=getLevel(voList,1L);
        List<SpecValueVo> voList2 =getLevel(voList,2L);
        for(SpecVo spec:list){
            SpecValueVo vo=new SpecValueVo();
            vo.setId(spec.getId());
            vo.setSpecId(spec.getId());
            vo.setSpecLevel(0L);
            vo.setIsEnable(WhetherEnum.YES);
            vo.setSpecValue(spec.getSpecName());
            List<SpecValueVo> child=Lists.newArrayList();
            for(SpecValueVo vo1:voList1){
                if(vo1.getParentId().equals(spec.getId())){
                    child.add(vo1);
                }
            }
            vo.setChildList(child);
            result.add(vo);
        }
        for(SpecValueVo valueVo:result){
            List<SpecValueVo> tt=valueVo.getChildList();
            for(SpecValueVo v1:valueVo.getChildList()){
                List<SpecValueVo> child=Lists.newArrayList();
                for(SpecValueVo vo1:voList2){
                    System.out.println(vo1.getParentId());
                    System.out.println(v1.getId());
                    if(vo1.getParentId().equals(v1.getId())){
                        child.add(vo1);
                    }
                }
                v1.setChildList(child);
            }
        }
        return result;
    }
    private List<SpecValueVo> getLevel(List<SpecValueVo> voList, Long i) {
        List<SpecValueVo> list=Lists.newArrayList();
        for(SpecValueVo vo:voList){
            if(vo!=null&&null!=vo.getSpecLevel()){
                if(vo.getSpecLevel().equals(i)){
                    list.add(vo);
                }
            }

        }
        return list;
    }

    private List<SpecValueVo> getChlid(List<SpecValueVo> chilidList,List<SpecValueVo> voList, Long specId) {
        for(SpecValueVo vo:voList){
            if(vo.getParentId().equals(specId)){
                chilidList.add(vo);
                getChlid(chilidList,voList,vo.getId());
            }
        }
        return chilidList;
    }
}
